package servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import pojo.PageTools;
import pojo.Student;
import pojo.User;
import service.Service;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/studentServlet")
public class studentServlet extends baseServlet{
    private Service service = new Service();
    //分页查询方法
//    public void dimQuery(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//        //获取当前页喝每页显示条数
//        String currentPage = req.getParameter("currentPage");
//        String pageSize = req.getParameter("pageSize");
//        // 处理当前页
//        int cp;
//        if (currentPage == null ||"".equals(currentPage)){
//            cp = 1;
//        }else{
//            cp = Integer.parseInt(currentPage);
//        }
//        //处理每页显示条数
//        int ps;
//        if (pageSize == null||"".equals(pageSize)){
//            ps = 3;
//        }else{
//            ps = Integer.parseInt(pageSize);
//        }
//        //总条数查询数据库
//        int sumSize = service.queryAllSize();
//        //封装分页工具类
//        PageTools pt = new PageTools(cp,ps,sumSize);
//        //调用service查询数据库
//        List<Student> lists = service.dimQueryStudent(pt);
//        // 要把分页工具类和 lists集合都返回回去
//        //将lists集合放到pt工具类中
//        pt.setStudentList(lists);
//        //转json
//        String json = new ObjectMapper().writeValueAsString(pt);
//        resp.getWriter().print(json);
//    }
//         模糊搜索
    public void dimQueryStudent(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        String sname = req.getParameter("sname");
        String sex = req.getParameter("sex");
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        // 处理当前页
        int cp;
        if (currentPage == null ||"".equals(currentPage)){
            cp = 1;
        }else{
            cp = Integer.parseInt(currentPage);
        }
        //处理每页显示条数
        int ps;
        if (pageSize == null||"".equals(pageSize)){
            ps = 3;
        }else{
            ps = Integer.parseInt(pageSize);
        }
        //总条数查询数据库
        int sumSize = service.dimQueryCountStudent(sname,sex);
        //封装分页工具类
        PageTools pt = new PageTools(cp,ps,sumSize);
        //调用service方法
        List<Student> lists = service.dimQueryStudent(sname,sex,pt);
        //将lists集合放到pt工具类中
        pt.setStudentList(lists);
        //转json
        String json = new ObjectMapper().writeValueAsString(pt);
        resp.getWriter().print(json);


    }
    public void selectUser(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{
        //设置字符集
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //获取
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        int list = service.selectUser(username,password);
            String json = new ObjectMapper().writeValueAsString(list);
            resp.getWriter().print(json);
    }
}
